﻿// Contacts tab

function populateContacts()
{
    if (!tab_contacts_loaded)
        show_wait("contacts_tab");
    osapi.http.get({
        'href': rest_people_friends,
        'format': 'json',
        'oauth_service_name': oauth_service_name,
        'authz': authz_type
    }).execute(fill_contacts);
}

function fill_contacts(data)
{
    show_errors_if_any(data);
    //saving hash in order to know when new data arrives, 
    //if the hash is the same when the tab is refreshed - no need to redraw it
    if (compare_and_save_hash(data.metadata.DataHash))
        return;
    show_wait("contacts_tab");

    var tab_container = $("contacts_tab_container");

    remove_all_children(tab_container);

    // container for retrieved data
    var container = new Element('div');
    container.addClassName('contentbox');
    tab_container.appendChild(container);

    i = 0;
    while (i < data.content.totalResults)
    {
        if (data.content.entry[i])
        {
            var contact = new Element('div');
            contact.addClassName("contact_mini");
            var photo = new Element('div');
            var img = new Element('img', { src: get_profile_image_url(data.content.entry[i].id) });
            photo.appendChild(img);
            photo.setStyle({ cssFloat: 'left' });
            contact.appendChild(photo);
            var details = new Element('a', { href: get_profile_url(data.content.entry[i].id), target: "_blank" });
            details.update(data.content.entry[i].displayName);
            details.setStyle({  });
            details.addClassName('contact_name');
            contact.appendChild(details);
            contact.innerHTML += "<div style='clear:both;'></div>";
            container.appendChild(contact);
        }
        i++;
    }
    hide_wait("contacts_tab");
    tab_contacts_loaded = true;

}

function update_contacts()
{
    $('update_image').setAttribute('src', update_image_moving_url);
    populateContacts();
    $('update_image').setAttribute('src', update_image_url);
}